package org.pratik.ghine;
import ClassesInfo.configuration;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Add_Answer
 */
@WebServlet("/Add_AnswerPath")
public class Add_Answer extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Statement st=null;
	private ResultSet result=null;	
	private RequestDispatcher addTopicJsp,indexDispatcher;
	private configuration conf;
    private int max_id;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Add_Answer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		conf=new configuration();
		System.out.println("hi from cardio");
		java.util.Date dt=new java.util.Date();
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String a_datetime=sdf.format(dt);
		String a_name=request.getParameter("a_name");
		String a_email=request.getParameter("a_email");
		String a_answer=request.getParameter("a_answer");
		String question_id=request.getParameter("id");
		
		Date now=new Date();
		
		try
		{
			
				Class.forName(conf.getDriver()).newInstance();				
				Connection con=null;		
				con =conf.getConnection();
				System.out.println(con);
				st=con.createStatement();
				 result=st.executeQuery("SELECT MAX(a_id) AS Maxa_id FROM forum_anwser WHERE question_id='"+question_id+"'");
				 if(result.next())
				 {
					 max_id=result.getInt("Maxa_id")+1;
					 
				 }
				 else
				 {
					 max_id=1;
				 }
				 int rs=st.executeUpdate("Insert into forum_anwser(question_id,a_id,a_name,a_email,a_answer,a_datetime) VALUES("+"'"+question_id+"','"+max_id+"','"+a_name+"',"+"'"+a_email+"','"+a_answer+"',"+"'"+a_datetime+"')");
				con.close();
				System.out.println(result);
				
		}// end of try
				catch(Exception e)
				{
					e.printStackTrace();
				}
			
		
		
	}

}
